package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by skyward on 2017/3/22.
 * email:
 */

public class MyLinearLayout extends ViewGroup {


    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();

        int width =0;
        int height=0;

        measureChildren(widthMeasureSpec,heightMeasureSpec);

        if(widthMode == MeasureSpec.AT_MOST){
            for (int i = 0; i < getChildCount(); i++) {
                View childAt = getChildAt(i);
                MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();

                int childWidth = childAt.getMeasuredWidth()+lp.leftMargin + lp.rightMargin;
                width = Math.max(childWidth,width);
            }
            width = width + pl+pr;
        }
        if(heightMode == MeasureSpec.AT_MOST){
            for (int i = 0; i < getChildCount(); i++) {
                View childAt = getChildAt(i);
                MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();
                height += childAt.getMeasuredHeight() + lp.topMargin+lp.bottomMargin;
            }
            height = height + pt + pb;
        }

        setMeasuredDimension(widthMode == MeasureSpec.AT_MOST ? width : widthSize,heightMode == MeasureSpec.AT_MOST ? height: heightSize);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int pl = getPaddingLeft();
        int pt = getPaddingTop();
        int pr = getPaddingRight();
        int pb = getPaddingBottom();

        int cl =0;
        int ct =0;
        int cr =0;
        int cb =0;
        int cm =0;

        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);

            MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();

            cl = lp.leftMargin;
            ct += lp.topMargin;
            cr = childAt.getMeasuredWidth() + lp.leftMargin;
            cb += childAt.getMeasuredHeight() + lp.topMargin;

            childAt.layout(cl+pl,ct+pt+cm,cr+pr+lp.rightMargin,cb+pb+cm);
            ct += childAt.getMeasuredHeight();
            cm += lp.bottomMargin;

        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }
}
